#include "MainMenuScene.hpp"

MainMenuScene::MainMenuScene()
{

}

MainMenuScene::MainMenuScene(int width, int height):Scene::Scene(width,height)
{
    btn_play = new Button(L"开始游戏",{100,100});
    btn_continue = new Button(L"继续游戏",{100,200});
    btn_option = new Button(L"游戏设置",{100,300});
    btn_exit = new Button(L"退出游戏",{100,400});

    SDL_Rect rc = btn_play->getRect();
    int center_x_begin = (this->width-rc.w)/2;
    btn_play->setPosition(center_x_begin,100);
    btn_continue->setPosition(center_x_begin,200);
    btn_option->setPosition(center_x_begin,300);
    btn_exit->setPosition(center_x_begin,400);
    this->components.push_back(btn_play);
    this->components.push_back(btn_continue);
    this->components.push_back(btn_option);
    this->components.push_back(btn_exit);
}

MainMenuScene::~MainMenuScene()
{
    for(Component* com:components){
        delete com;
    }
    components.clear();
}

int MainMenuScene::handleEvent(SDL_Event &evt)
{
    int res=0;
    for(Component* com:components){
        res=com->handleEvent(evt);
        if(res){
            if(evt.type==SDL_MOUSEBUTTONUP){
                if(com==btn_exit){
                    return -2;
                }
            }
        }
    }
    return 0;
}

void MainMenuScene::render(SDL_Renderer *renderer)
{
    for(Component* com:components){
        com->render(renderer);
    }
}
